 ********************************************************;
** Generate conditions to flag ICD9-CM procedure and or diagnostic candidates  **;
** *****************************************************;
**  Used by the candidate programs. Create a if then  **;
**  condition which describes the index event either in*;
**  terms of diagnostic and/or procedure code.         *;
**  Make sure to reference the same cohort in this file*;
**  as you do in all other files.                    ***;
** *****************************************************;
** Note you can also put conditions specific to a file  ;
** as has been in the AMI example to drop SNF clms      ;
** NEW CODE ; 
********************************************************;

%macro ICD9cohrtDef(cohrt= ,file= ,numDgnCodes= ,numProcCodes=,prcdPfx= ,dgnsPfx= );
    ****************************************************************;
    ***  SET UP ARRAYS AND VARIABLES TO BE FILLED IN;
    ****************************************************************;
    %if &numProcCodes. > 0  %then %do;
        array prcdr_cd_arr  (&numProcCodes.)
            &prcdPfx.1-&prcdPfx.&prcPos.;
        array prcdr_cnt_arr (&numProcCodes.)
            %RepeatN(tagPrc,1,&numProcCodes.);
        do i=1 to &numProcCodes.;
            prcdr_cnt_arr(i)=0;
            end;
        %end;
    %if &numDgnCodes. > 0  %then %do;
        array dgns_cd_arr   (&numDgnCodes.)
            %RepeatN(&dgnsPfx.,1,&numDgnCodes.);
        array dgns_cnt_arr  (&numDgnCodes.)
            %RepeatN(tagDgn,1,&numDgnCodes.);
        do i=1 to &numDgnCodes.;
            dgns_cnt_arr(i)=0;
            end;
        %end;
    ***!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!***************;
    ***  PLACE CONDTIONS UNDER THE ARRAY CALLS;
    ***!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!***************;

    ****************************************************************;
    ***  DEFINES ALL STENT CODE - BASED ON A  PROCEDURE SEQUENCE;
    ****************************************************************;
    %if &cohrt. =allstent %then %do;
        %if &numProcCodes.>0 %then %do;
        do i=1 to &numProcCodes.;
            if substr(prcdr_cd_arr(i),1,2) = "36" then do;
                prcdr_cnt_arr(i)=1;
                ** Makes sure we take the lowest procedure number in codes 1 - 6;
                if pICD9_indx = "" then prcIndxNum=i;
                if pICD9_indx = "" then pICD9_indx=prcdr_cd_arr(i);
                end;
            end;
        %end;
    %end;
    ****************************************************************;
    ***  DEFINES STENT CODE - BASED ON A  PROCEDURE SEQUENCE;
    ****************************************************************;
    %if &cohrt. =stent %then %do;
        %if &numProcCodes.>0 %then %do;
        do i=1 to &numProcCodes.;
            if (length(prcdr_cd_arr(i)) =4 ) and
                (substr(prcdr_cd_arr(i),1,4) = "3606" or substr(prcdr_cd_arr(i),1,4) = "3607" )then do;
                prcdr_cnt_arr(i)=1;
                pICD9_indx=prcdr_cd_arr(i);
                prcIndxNum=i;
                end;
            end;
        %end;
    %if &numDgnCodes.>0 %then %do;
        do i=1 to &numDgnCodes.;
                **LOOP THROUGH DIAGNOSTIC CODES;
            if substr(dgns_cd_arr(i),1,3) = "410" and
                ((length(dgns_cd_arr(i)) = 5 and substr(dgns_cd_arr(i),5,1) ne "2") or
                (length(dgns_cd_arr(i)) < 5) )
                then    do    ;
                dgns_cnt_arr(i)=1;
                dgnIndxNum=i;
                dICD9_indx=dgns_cd_arr(i);
                end;
            end;
        %end;
    %end;
    ****************************************************************;
    ***  DEFINES HIP ICD9-CM CODE- BASED ON DIAGNOSTIC SEQUENCE;
    ****************************************************************;
    %if &cohrt. = hip  %then %do;
        %if &numDgnCodes.>0 %then %do;
        do i=1 to &numDgnCodes.;
            if substr(dgns_cd_arr(i),1,3) = "820" then do;
                dgns_cnt_arr(i)=1;
                dgnIndxNum=i;
                dICD9_indx=dgns_cd_arr(i);
            end;
        end;
	%if &file. = medpar %then %do;
		**Index events only at short term /CAH;
		if is_shortterm=0 and is_cah=0 then delete;
		%end;               
        %end;
    %end;
    ****************************************************************;
    ***  DEFINES CHF ICD9-CM CODE - BASED ON DIAGNOSTIC SEQUENCE;
    ****************************************************************;
    %if &cohrt. = chf  %then %do;
        %if &numDgnCodes.>0 %then %do;
        do i=1 to &numDgnCodes.;
            if  (substr(dgns_cd_arr(i),1,3) = "428")
                then do;
                dgns_cnt_arr(i)=1;
                dgnIndxNum=i;
                dICD9_indx=dgns_cd_arr(i);
                end;
            end;
 	 %if &file. = medpar %then %do;
		** Index events only at short-term / CAH;
		if is_shorterm=0 and is_cah=0 then delete;
		%end;           
        %end;
    %end;
    ****************************************************************;
    ***  DEFINES PNEUMONIAS ICD9-CM CODE - BASED ON DIAGNOSTIC SEQUENCE;
    ****************************************************************;
    * codes are based on table d.3.1 from quality net;

    %if &cohrt. = pnu  %then %do;
        %if &numDgnCodes.>0 %then %do;
        do i=1 to &numDgnCodes.;


            if  dgns_cd_arr(i) in("00322","0203","0204","0205","0212","0221","0310","0391",
                                              "0521","0551","0730","0830","1124","1140","1144","1145",
                                              "11505","11515","11595","1304","1363","4800","4801","4802",
                                              "4803","4808","4809","481","4820","4821","4822","4823","48230",
                                              "48231","48232","48239","4824","48240","48241","48242"
                                              "48249","4828","48281","48282","48283","48284","48289","4829",
                                              "483","4830","4831","4838","4841","4843","4845","4846","4848",
                                              "4847","485","486","5130","5171")
                then do;
                dgns_cnt_arr(i)=1;
                dgnIndxNum=i;
                dICD9_indx=dgns_cd_arr(i);
                end;
            end;
        %if &file. = medpar %then %do;
            **Drop if index event occurs at SNF;
            if is_shortterm=0 and is_cah=0 then delete;
            %end;

        %end;
    %end;

     ****************************************************************;
     ***  HIP/KNEE REPLACEMENT
     ****************************************************************;
    %if &cohrt. = hk  %then %do;
        ***!!!!!!!!!!!!! INSERT PROCEDURE CODE OF INTEREST- ONLY IF NUMDGN CODES >0;
        %if &numProcCodes.>0 %then %do;
            **LOOP THROUGH PROCEDURE CODES;
            do i=1 to &numProcCodes.;
              if prcdr_cd_arr(i) = "8154" or prcdr_cd_arr(i) = "8151"
                 then do;
                  prcdr_cnt_arr(i)=1;
                  pICD9_indx=prcdr_cd_arr(i);
                  prcIndxNum=i;
                end;
            end;
        %if &file. = medpar %then %do;
            **LIMIT TO STAYS IN SHORT TERMS HOSP;
            if is_shortterm=0 and is_cah=0 then delete;
            %end;

           %end;
        %end;

     ****************************************************************;
     ***  DEFINES AMI ICD9-CM CODE - BASED ON DIAGNOSTIC SEQUENCE ;
     ***  WITH PROCEDURE EXAMPLE COMMENTED OUT;
     ****************************************************************;
    %if &cohrt. = ami  %then %do;
          ***!!!!!!!!!!!!! DEAL WITH DIAGNOISTIC CODE- ONLY IF NUMDGN CODES >0;
            %if &numDgnCodes.>0 %then %do;
            do i=1 to &numDgnCodes.;
                **LOOP THROUGH DIAGNOSTIC CODES;
                if substr(dgns_cd_arr(i),1,3) = "410" and
                    ((length(dgns_cd_arr(i)) = 5 and substr(dgns_cd_arr(i),5,1) ne "2") or
                    (length(dgns_cd_arr(i)) < 5) )
                    then    do    ;
                    dgns_cnt_arr(i)=1;
                    dgnIndxNum=i;
                    dICD9_indx=dgns_cd_arr(i);
                    end;
            **Make sure that we are dropping  410.X2;
                if (substr(&dgnsPfx.1,5,1) = "2" and length(dgns_cd_arr(i)) = 5 ) then delete;
                end;
            %end;
        **You can put other options specific to claims file here;
       %if &file. = medpar %then %do;
            **Drop if index event occurs at SNF;
            if is_shortterm=0 and is_cah=0 then delete;
            %end;
         %end;
         
 	*********************************************************************;
	**** DEFINES STROKE ICD-9CM CODE - BASED ON DIAGNOSTIC SEQUENCE ;	
	%if &cohrt. = stk  %then %do;
	*** DEAL WITH DIAGNOSTIC CODE - ONLY IF NUMDGN CODES > 0; 
            %if &numDgnCodes.>0 %then %do;
	      do i=1 to &numDgnCodes.;
	      ** LOOP through diagnosis codes;  
		if  (dgns_cd_arr(i) = "430")
		    	or (dgns_cd_arr(i) = "431")
		    	or (dgns_cd_arr(i) = "4320")
			or (dgns_cd_arr(i)= "4321")
			or (dgns_cd_arr(i) = "4329")
			or (dgns_cd_arr(i) = "43301")
			or (dgns_cd_arr(i) = "43311")
			or (dgns_cd_arr(i) = "43321")
			or (dgns_cd_arr(i) = "43331")
			or (dgns_cd_arr(i) = "43381")
			or (dgns_cd_arr(i) = "43391")
			or (dgns_cd_arr(i) = "43401")
			or (dgns_cd_arr(i) = "43411")
			or (dgns_cd_arr(i) = "43491")
			or (dgns_cd_arr(i) = "436") 		
		    then do;
		    dgns_cnt_arr(i) = 1;
		    dgnIndxNum = i;
		    dICD9_indx = dgns_cd_arr(i);
                    end;
		    end;
		%end; 

	       **Make sure that we are dropping SNF claims;
		   %if &file. = medpar %then %do;
                      if is_shortterm=0 and is_cah=0 then delete;
		   %end;
		%end;         
         
         
    ****************************************************************;
    ***  DEFINES ACS ICD9-CM CODE SEQUENCE - DIAGNOSTIC EXAMPLE;
    ****************************************************************;
    %if &cohrt. = acs  %then %do;
        %if &numDgnCodes.>0 %then %do;
        do i=1 to &numDgnCodes.;
            if (substr(dgns_cd_arr(i),1,3) = "410")
                or  (substr(dgns_cd_arr(i),1,3) = "411")
                or  (substr(dgns_cd_arr(i),1,3) = "413")
                or  (substr(dgns_cd_arr(i),1,3) = "414")
                then do;
                dgns_cnt_arr(i)=1;
                dgnIndxNum=i;
                dICD9_indx=dgns_cd_arr(i);
                end;
            end;
        %end;
    %end;
    ****************************************************************;
    ***  DEFINES COMPLICATED AMI CASE WITH DGN AND PRC  ICD9-CM CODE SEQUENCE - DIAGNOSTIC EXAMPLE;
    ****************************************************************;
%if &cohrt. = bami  %then %do;
          ***!!!!!!!!!!!!! DEAL WITH DIAGNOISTIC CODE- ONLY IF NUMDGN CODES >0;
        %if &numDgnCodes.>0 %then %do;
            do i=1 to &numDgnCodes.;
                **LOOP THROUGH DIAGNOSTIC CODES -- CHOOSE AMI OCCRUING IN PRINCIPAL CODE;
                if (substr(dgns_cd_arr(1),1,3) = "410" and
                    ((length(dgns_cd_arr(1)) = 5 and substr(dgns_cd_arr(1),5,1) ne "2") or
                    (length(dgns_cd_arr(1)) < 5) ))
                    then    do    ;
                    dgns_cnt_arr(1)=1;
                    dgnIndxNum=1;
                    dICD9_indx=dgns_cd_arr(1);
                    end;
                else if  (substr(dgns_cd_arr(i),1,3) = "411" and
                    ((length(dgns_cd_arr(i)) = 5 and substr(dgns_cd_arr(i),5,1) ne "2") or
                    (length(dgns_cd_arr(i)) < 5) ))
                    then    do    ;
                    dgns_cnt_arr(i)=1;
                    dgnIndxNum=i;
                    dICD9_indx=dgns_cd_arr(i);
                    end;

            **Make sure that we are dropping  410.X2;
                if (substr(&dgnsPfx.1,5,1) = "2" and length(dgns_cd_arr(i)) = 5 ) then delete;
                end;
            %end;
        ***!!!!!!!!!!!!! INSERT PROCEDURE CODE OF INTEREST- ONLY IF NUMDGN CODES >0;
        %if &numProcCodes.>0 %then %do;
            **LOOP THROUGH PROCEDURE CODES;
            do i=1 to &numProcCodes.;
               if (length(prcdr_cd_arr(i)) =4 ) and
                (substr(prcdr_cd_arr(i),1,4) = "3606" or substr(prcdr_cd_arr(i),1,4) = "3607" or
                 substr(prcdr_cd_arr(i),1,3) = "360"  or substr(prcdr_cd_arr(i),1,3) = "361" or
                 substr(prcdr_cd_arr(i),1,4) = "3722" or substr(prcdr_cd_arr(i),1,4) = "3723" or
                 substr(prcdr_cd_arr(i),1,4) = "8855" or substr(prcdr_cd_arr(i),1,4) = "8856" or
                 substr(prcdr_cd_arr(i),1,4) = "8857"
                ) then do;
                prcdr_cnt_arr(i)=1;
                pICD9_indx=prcdr_cd_arr(i);
                prcIndxNum=i;
                end;
             end;
            %end;
        **You can put other options specific to claims file here;
        %if &file. = medpar %then %do;
            **Drop if index event occurs at SNF;
            if is_snf=1 then delete;
            %end;
        %end;

    %mend;
